<template>
  <div>
    <el-dropdown>
      <div class="icon-box">
        <span>{{languageName}}</span>
        <i class="el-icon-arrow-down"></i>
      </div>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item
          v-for="v in options"
          :key="v.code"
          @click.native="changeLanguage(v)"
        >
          {{ v.languageValue || v.remark }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
import { computed} from 'vue'

export default {
  name: 'BaseLanguageDropdown',
  props: {
    lang: {
      type: String,
      default: 'zh',
    },
    options: {
      type: Array,
      default: () => [],
    }
  },
  emits: ['change'], 
  setup(props, { emit }){
    const languageName = computed(() => {
      const item_ = props.options.find(v=>v.language === props.lang)
      return item_?.languageValue || item_?.remark || '简体中文'
    });
    // 切换语言
    const changeLanguage = (langOption) => {
      const lang = langOption.language
      emit('change', lang);
    }

    return {
      languageName,
      changeLanguage,
    }
  }, 
}
</script>

<style scoped lang='scss'>
  .icon-box{
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;

    .el-icon-arrow-down{
      margin-left: 6px;
      font-size: 18px;
    }
  }
</style>